<?php
namespace app\admin\controller;
use think\Db;
class City extends SessionCheck
{
    public function index()
    {
	    if(session('type') == 'all'){
			$data = Db::table("province")->order('id asc')->select();
		}else{
		    if(session('type') == 'province'){
                $data = Db::table('province')->whereIn('id',session('aid'))->order('id asc')->select();
            }elseif(session('type') == 'city'){
		        $father_id = Db::table('city')->where('id',session('this_aid'))->value('father_id');
                $data = Db::table('province')->whereIn('id',$father_id)->order('id asc')->select();
            }elseif(session('type') == 'area'){
                $father_id = Db::table('area')->where('id',session('this_aid'))->value('father_id');
                $parent_id = Db::table('city')->where('id',$father_id)->value('father_id');
                $data = Db::table('province')->whereIn('id',$parent_id)->order('id asc')->select();
            }
		}
        return view('',['data' => $data,'menu_list'=>$this->menu_list]);
    }

    public function ajax_get()
    {
        $type = input('get.type');
        $father_id = input('get.id');
        $nofather = request()->request('nofather');
        $where = [];
        if (!empty($father_id)) {
            if ($nofather == 'ok') {
                $where['id'] = $father_id;
            } else {
                $where['father_id'] = $father_id;
            }
        }
        if($type != 'province'){
            if($father_id == '')
                die;
        }
        if (session('type') == 'all') {
            $data = Db::table($type)->where($where)->order('id asc')->select();
        } else {
            if ($type != 'area') {
                $where = [];
                $where['status'] = 1;
            }
            $data = Db::table($type)->whereIn('id', session('aid'))->where($where)->order('id asc')->select();
        }
        return json(['data' => $data]);
    }

	
	public function status()
    {
		$type = input('get.type');
		$id = input('get.id');
		$status = input('get.status');
		switch ($type)
		{
		case 'province':
			if(session('type') != 'all'){
				if(!in_array($id,explode(',',session('aid')))){
					break;
				}
			}
			Db::table($type)->where(['id'=>$id])->setField('status',$status);
			Db::table('city')->where(['father_id'=>$id])->setField('status',$status);
			$city = Db::table('city')->where(['father_id'=>$id])->column('id');
			Db::table('area')->whereIn('father_id',$city)->setField('status',$status);
			break;
		case 'city':
			if(session('type') != 'all'){
				if(!in_array($id,explode(',',session('aid')))){
					break;
				}
			}
		   Db::table($type)->where(['id'=>$id])->setField('status',$status);
		   Db::table('area')->where(['father_id'=>$id])->setField('status',$status);
		   break;
		case 'area':
			if(session('type') != 'all'){
				if(!in_array($id,explode(',',session('aid')))){
					break;
				}
			}
			Db::table($type)->where(['id'=>$id])->setField('status',$status);
			break;
		default:
		    echo "No number between 1 and 3";
		}
		$this->redirect(url('/admin/city'));
		exit;
    }
}
